﻿using System.Web.Mvc;
using Kratos.Framework.ContentManagement.Models.Order;
using Kratos.Framework.Filters;
using Kratos.Framework.Modules.Core.Controllers;
using Kratos.Modules.Order.Services;
using MvcContrib;

namespace Kratos.Modules.Order.Controllers
{
    public class ProductController : KratosController
    {
        private readonly IProductService _productServce;

        public ProductController(IProductService productServce)
        {
            _productServce = productServce;
        }

        public ActionResult Index(int? id)
        {
            if (!id.HasValue)
            {
                var model = _productServce.GetAll();
                return View("Index", model);
            }
            else
            {
                var model = _productServce.GetProductById(id.Value);
                return View("ProductInfo", model);
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [UnitOfWork]
        public ActionResult Create(Product product)
        {
            _productServce.Save(product);
            return this.RedirectToAction(c => c.Detail(product.ProductId));
        }

        public ActionResult Detail(int id)
        {
            var product = _productServce.GetProductById(id);
            return View(product);
        }
    }
}
